package com.naiyumie.ncalculator;

import java.math.BigDecimal;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class PercentMultiple01Activity extends KingWangJjangActivity implements OnClickListener {
	
	String msg = null;
	EditText ete1 = null, ete2 = null;
	TextView tve1 = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_percent_multiple_01);
		Intent fromIntent = getIntent();
		msg = fromIntent.getStringExtra("MSG");
		this.setMsg(msg);
		this.callMethod();
		tvTitle.setText(R.string.category03);
		ete1 = (EditText) findViewById(R.id.ete1);
		ete1.setFilters(new InputFilter[] { nWidgetTool.regEditTextFilter("^[0-9]+$") });
		ete1.addTextChangedListener(percentMultipleWatcher);
		ete2 = (EditText) findViewById(R.id.ete2);
		ete2.addTextChangedListener(percentMultipleWatcher);
		ete2.setFilters(new InputFilter[] { nWidgetTool.regEditTextFilter("^[,0-9]+$") });
		tve1 = (TextView) findViewById(R.id.tvHelperText);
	}
	
	private final TextWatcher percentMultipleWatcher = new TextWatcher() {
		
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
		
		public void afterTextChanged(Editable s) {
			String totalValue = ete1.getText().toString();
			String inputValue = ete2.getText().toString();
			if (!Pattern.matches("^[0-9]+$", totalValue)) {
				nlog.puts("invalid totalValue");
				return;
			}
			if (!Pattern.matches("^[,0-9]+$", inputValue)) {
				nlog.puts("invalid inputValue");
				return;
			}
			String result = "";
			BigDecimal totalValueBigDecimal = BigDecimal.ZERO;
			BigDecimal inputtedTotalValue = BigDecimal.ZERO;
			BigDecimal totalPercent = BigDecimal.ZERO;
			try {
				String regExp = ",";
				String[] inputValueArray = inputValue.split(regExp);
				totalValueBigDecimal = new BigDecimal(totalValue);
				for (int i = 0; i < inputValueArray.length; i++) {
					inputtedTotalValue = inputtedTotalValue.add(new BigDecimal(inputValueArray[i]));
				}
				result += "[입력값과 퍼센트] \n";
				for (int i = 0; i < inputValueArray.length; i++) {
					BigDecimal partValue = new BigDecimal(inputValueArray[i]);
					BigDecimal calculate = partValue.divide(totalValueBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
					BigDecimal calculateResult = calculate.multiply(new BigDecimal(100));
					totalPercent = totalPercent.add(calculateResult);
					result += String.format("%d : 전체값 %s의 %s은 %s%% 입니다.\n", i + 1,
							totalValueBigDecimal.toString(),
							inputValueArray[i].toString(), String.valueOf(calculateResult));
				}
			} catch (Exception e) {
				nlog.puts("error");
			} finally {
				result += "\n";
				String remainPercent = totalPercent.toString();
				result += String.format("입력값의 총합 : %s, 전체값까지 : %s, 총 퍼센트 : %s%%\n", inputtedTotalValue.toString(), totalValueBigDecimal.subtract(inputtedTotalValue).toString(), remainPercent);
				tve1.setText(result);
			}
		}
	};
}
